package com.ls.springbootdemo.util;

import com.ls.springbootdemo.common.exception.MyException;
import com.ls.springbootdemo.common.request.MaskFieldReq;
import com.sun.xml.internal.messaging.saaj.util.Base64;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 图片工具
 *
 * @author 挥之以墨
 */
@Slf4j
public class ImgUtil {
    /**
     * 通过url解析并压缩图片
     *
     * @param imgUrl 图片地址
     * @return 身份证图片base64
     */
    public static String imgToBase64ByUrl(String imgUrl) {
        try {
            URL url = new URL(imgUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);

            try (ByteArrayOutputStream bout = new ByteArrayOutputStream();
                 InputStream in = conn.getInputStream()) {
                return imgToBase64(in, bout);
            }
        } catch (IOException e) {
            log.error("压缩身份证图片失败：");
            throw new MyException("获取身份证失败", e);
        }
    }

    /**
     * 通过硬盘路径解析并压缩图片
     *
     * @param path 硬盘路径
     * @return 身份证图片base64
     */
    public static String imgToBase64ByPath(String path) {
        File file = new File(path);
        try (ByteArrayOutputStream bout = new ByteArrayOutputStream();
             InputStream in = new FileInputStream(file)) {
            return imgToBase64(in, bout);
        } catch (IOException e) {
            log.error("压缩身份证图片失败：");
            throw new MyException("获取身份证失败", e);
        }
    }

    /**
     * 压缩图片
     */
    private static String imgToBase64(InputStream in, ByteArrayOutputStream bout) throws IOException {
        Thumbnails.of(in).size(1024, 768).outputQuality(0.5f).toOutputStream(bout);
        return new String(Base64.encode(bout.toByteArray()));
    }

    /**
     * 组装身份证信息
     *
     * @param frontUrl 身份证正面url
     * @param backUrl  身份证背面url
     */
    public static List<MaskFieldReq> buildMaskFieldByUrl(String frontUrl, String backUrl) {
        return buildMaskField(ImgUtil.imgToBase64ByUrl(frontUrl), ImgUtil.imgToBase64ByUrl(backUrl));
    }

    /**
     * 组装身份证信息
     *
     * @param frontPath 身份证正面Path
     * @param backPath  身份证背面Path
     */
    public static List<MaskFieldReq> buildMaskFieldByPath(String frontPath, String backPath) {
        return buildMaskField(ImgUtil.imgToBase64ByPath(frontPath), ImgUtil.imgToBase64ByPath(backPath));
    }

    /**
     * 组装身份证信息
     *
     * @param frontBase64 身份证正面base64
     * @param backBase64  身份证背面base64
     */
    private static List<MaskFieldReq> buildMaskField(String frontBase64, String backBase64) {
        List<MaskFieldReq> maskFieldReqList = new ArrayList<>();
        MaskFieldReq maskFieldReq = MaskFieldReq.builder()
                .imgInf1(frontBase64)
                .imgInf2(backBase64)
                .build();
        maskFieldReqList.add(maskFieldReq);
        return maskFieldReqList;
    }

    public static void main(String[] args) {
        String s = ImgUtil.imgToBase64ByPath("E:\\sfz\\5m-back.jpg");
        String base64Decode = Base64.base64Decode(s);
        File file = new File("E:\\sfz\\5m-back-test.jpg");
        try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
            fileOutputStream.write(base64Decode.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

        s = ImgUtil.imgToBase64ByUrl("https://cdzg-app20.oss-cn-beijing.aliyuncs.com/test/customer/211173825193193472/productImage/productImage_1586418909373.png");
        base64Decode = Base64.base64Decode(s);
        file = new File("E:\\sfz\\url-test2.jpg");
        try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
            fileOutputStream.write(base64Decode.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
